package com.ljx.listenter;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.exception.GuliException;
import com.ljx.pojo.EduSubject;
import com.ljx.pojo.excel.ExcelSubjectData;
import com.ljx.service.EduSubjectService;
import com.ljx.service.impl.EduSubjectServiceImpl;

/**
 * @author 李加喜
 * @date 2020/9/24 0024 22:20
 * @Email 1129071273@qq.com
 */

public class ExcelSubjectListener extends AnalysisEventListener<ExcelSubjectData> {
    //这个对象是不能交给Spring管理的  所以不能通过注解引入service
    //需要将数据添加到数据库里面所以需要使用service
    //因为ExcelSubjectListener不能交给spring管理需要自己new,不能注入其他的对象
    //不能实现对于数据库的操作
    public EduSubjectServiceImpl eduSubjectService;
    public ExcelSubjectListener (){}
    public ExcelSubjectListener(EduSubjectServiceImpl eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }
    //因为不能通过注解的方式将eduSubjectService进行注入 所以需要通过传值的方式将eduSubjectService进行注入
    //根据ExcelSubjectData里面的设置
    @Override
    public void invoke(ExcelSubjectData excelSubjectData, AnalysisContext analysisContext) {
        //一行一行的读取数据
        if (excelSubjectData==null){
            throw new GuliException(20001,"没有内容");
        }
        //先判断一级分类是否重复
        EduSubject existOneSubject = this.existOneSubject(eduSubjectService, excelSubjectData.getOneSubjectName());
        if (existOneSubject==null){ //表示表里面没有重复的数据
            existOneSubject=new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(excelSubjectData.getOneSubjectName());//一级分类的名称  就是第一列的名字
            eduSubjectService.save(existOneSubject);
            //这个EduSubject的 ParentId和title设置了
        }
        //获取1级分类的Pid
        String parentId = existOneSubject.getId();
        //添加二级分类
        //判断二级分类是否重复
        EduSubject existTwoSubject = this.existTwoSubject(eduSubjectService, excelSubjectData.getTwoSubjectName(), parentId);
        if (existTwoSubject==null){
            existTwoSubject=new EduSubject();
            existTwoSubject.setParentId(parentId);
            existTwoSubject.setTitle(excelSubjectData.getTwoSubjectName());//获取二级分类的名字
            eduSubjectService.save(existTwoSubject);
        }

    }
    //判断一级分类不能重复添加  判断表里面是否存在这样的数据
    private EduSubject existOneSubject(EduSubjectService eduSubjectService,String name){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject oneSubject = eduSubjectService.getOne(wrapper);
        return oneSubject;
    }
    //判断二级分类不能重复添加
    private EduSubject existTwoSubject(EduSubjectService subjectService,String name,String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
